$(document).ready(function() {
	ShopCar.ClickSettle();
})

var v_shop_car = new Vue({
	el: '#v_shop_car',
	created() {
		var that = this;
		var uid = that.getCookie('uid')
		$.ajax({
			type: "post",
			url: "http://www.ibabypet.com.cn/public/index.php/api/goods/cart_list/",
			async: true,
			data: {
				uid: uid
			},
			success: function(res) {
				res = JSON.parse(res);
				console.log('购物车商品res', res)
				if(res.code == 200) {
					var arr = res.data;
					$.each(arr, function(i, v) {
						if(v.num == 0) {
							v.num = 1;
						};
						v.tprice = v.sell_price;
					});
					v_shop_car.shop_car_goods_list = arr;
					var shop_car_num = res.data.length;
					$('.car_product_num').text(shop_car_num);
				} else {
					v_shop_car.shop_car_goods_list = [];
				}
			}
		});
	},
	data: {
		shop_car_goods_list: [], //商品数据列表
		checked_list: [], //选中的商品的id
		checkAll: '',
		checked_goods_num: 0, //已选商品数量
		total_price: 0, //支付总价
	},
	watch: {
		checked_list: {
			handler: function(val, oldval) {
				//1：支付总额计算
				var that = this;
				that.total_price = 0;
				$.each(val, function(i, v) {
					$.each(that.shop_car_goods_list, function(index, value) {
						if(v == value.id) {
							that.total_price += parseInt(value.tprice);
						}
					});
				});
				//2： 全选按钮 判断是否选中
				var goods_len = v_shop_car.shop_car_goods_list.length;
				var checked_goods_len = val.length;
				v_shop_car.checked_goods_num = checked_goods_len; //已选商品数量
				if(checked_goods_len == goods_len) {
					v_shop_car.checkAll = true;
				} else {
					v_shop_car.checkAll = false;
				}
			}
		},
		shop_car_goods_list: {
			handler: function(val, oldval) {
				var that = this;
				that.total_price = 0;
				$.each(that.checked_list, function(i, v) {
					$.each(that.shop_car_goods_list, function(index, value) {
						if(v == value.id) {
							that.total_price += parseInt(value.tprice);
						}
					})
				})
			},
			deep: true
		}
	},
	methods: {
		//全选按钮
		checkedAll: function() {
			var that = this;
			if(that.checkAll == true) {
				var goods_list_arr = that.shop_car_goods_list;
				that.checked_list = [];
				$.each(goods_list_arr, function(i, v) {
					that.checked_list.push(v.id);
				})
			} else {
				that.checked_list = [];
			}
		},
		//单个商品数量更改 事件
		change_nums: function(list) {
			var that = this;
			if(list.num < 1) {
				list.num = 1;
			};
			list.tprice = list.sell_price * list.num;

		},
		addOne: function(list) {
			list.num++;
			list.tprice = list.sell_price * list.num;
		},
		minusOne: function(list) {
			list.num--;
			if(list.num < 1) {
				list.num = 1;
			};
			list.tprice = list.sell_price * list.num;
		},
		//点击选择选择商品
		get_tprice: function(tprice, checked_list, list) {
			var that = this;
			that.total_price = 0;
			$.each(checked_list, function(i, v) {
				$.each(list, function(index, value) {
					var gid = value.id;
					if(v == gid) {
						that.total_price += parseInt(value.tprice);
					}
				});
			});
		},
		//删除单个商品
		del_single_good: function(id) {
			var that = this;
			that.$confirm('此操作将永久删除商品, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				$.ajax({
					type: "post",
					url: 'http://www.ibabypet.com.cn/public/index.php/api/goods/delete_cart/',
					async: true,
					data: {
						id: id
					},
					success: function(res) {
						res = JSON.parse(res);
						if(res.code == 200) {
							that.$message(res.msg)
							ShopCar.init_page();
						}
					}
				});
			}).catch(() => {

			});

		},
		//获取cookie
		getCookie: function(cname) {
			var name = cname + "=";
			var ca = document.cookie.split(';');
			for(var i = 0; i < ca.length; i++) {
				var c = ca[i];
				while(c.charAt(0) == ' ') c = c.substring(1);
				if(c.indexOf(name) != -1) return c.substring(name.length, c.length);
			}
			return "";
		},

	}
})
var ShopCar = {
	url_api: 'http://www.ibabypet.com.cn/public/index.php/api/goods/cart_list/', //购物车列表  （uid 用户uid）
	url_api2: 'http://www.ibabypet.com.cn/public/index.php/api/goods/delete_cart/', //删除购物车商品 （id 购物车id）
	url_api3: 'http://qxu2147650096.my3w.com/public/index.php/api/order/add_pcorder/', //购买下单
	//获取cookie
	getCookie: function(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while(c.charAt(0) == ' ') c = c.substring(1);
			if(c.indexOf(name) != -1) return c.substring(name.length, c.length);
		}
		return "";
	},
	init_page: function() {
		var that = this;
		var uid = that.getCookie('uid')
		$.ajax({
			type: "post",
			url: that.url_api,
			async: true,
			data: {
				uid: uid
			},
			success: function(res) {
				res = JSON.parse(res);
				console.log('购物车商品res', res)
				if(res.code == 200) {
					var arr = res.data;
					$.each(arr, function(i, v) {
						if(v.num == 0) {
							v.num = 1;
						};
						v.tprice = v.sell_price;
					});
					v_shop_car.shop_car_goods_list = arr;
					var shop_car_num = res.data.length;
					$('.car_product_num').text(shop_car_num);
				} else {
					v_shop_car.shop_car_goods_list = [];
				}
			}
		});
	},
	//点击结算
	ClickSettle: function() {
		$('.settlement button').on('click', function() {
			$('.payment_modal, .payment_scan_wrap').show();
		});
		$('.payment_modal').on('click', function() {
			$('.payment_modal, .payment_scan_wrap').hide();
		})
	}

}